package com.example.sky20.servlet;

import com.example.sky20.Bean.AdminBean;
import com.example.sky20.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/adminAdd.jsp")
public class AdminAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String User = (String) session.getAttribute("user");
        if (User == null || User.equals("")){
            resp.sendRedirect("login.jsp");
            return;
        }
        req.setCharacterEncoding("UTF-8");
        AdminBean adminBean = new AdminBean();
        String pwd = req.getParameter("AdminPwd");
//        String type = req.getParameter("AdminType");
        adminBean.setAdminPwd(pwd);
        adminBean.setAdminType(1);
        adminBean.setAdminName("admin");//todo更换为登陆的管理员名称
        //添加
        AdminDao adminDao = new AdminDao();
        int result = adminDao.add(adminBean);//返回受影响的行
        if (result > 0) {//成功重新跳转到新闻页面
            resp.sendRedirect("admin.jsp");
        }else {//失败跳转错误页面 显示错误信息 并回到指定页面
            req.setAttribute("msg","添加失败");
            req.setAttribute("url","news.jsp");
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}